let drawQrcode = require("../../../../utils/weapp.qrcode.min.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lessonid: '',
    ewmkey: '',
    teaSignKey: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    that.setData({
      lessonid: e.lessonid,
    })

    this.getTeaEwm();

    //只允许打开相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success: function (res) {
        console.log(res)
        that.setData({
            ewmkey: res["result"]
          }),
          console.log(that.data.ewmkey)
      },
      fail: function () {
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        })
        wx.navigateBack({
          delta: 0,
          success: (res) => {},
          fail: (res) => {},
          complete: (res) => {},
        })
      },
      complete: function () {}
    })

  },

  getTeaEwm: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.ServerUrl + 'ewm/getTeaEwm.php',
      data: {
        lessonid: this.data.lessonid,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        that.setData({
            teaSignKey: res.data.ewm
          }),
          console.log(that.data.teaSignKey)
      },
    })
  },

  ewmsign: function (e) {
    var that = this;
    var userid = wx.getStorageSync('openid');
    if (that.data.ewmkey === that.data.teaSignKey) {
      wx.request({
        url: app.globalData.ServerUrl + 'ewm/stuSign.php',
        data: {
          lessonid: this.data.lessonid,
          userid: userid
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data)
          if (res.data) {
            wx.showToast({
              title: '签到成功',
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '签到失败',
              icon: 'none'
            })
          }
        },
      })
    }else{
      wx.showToast({
        title: '请扫描正确的二维码哟',
        icon: 'none'
      })
    }
  },



  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
})